﻿using UnityEngine;

namespace IQIGame.Onigao.Framework
{
    /// <summary>
    /// 类名 : 自定义-简单的进度条
    /// 作者 : Canyon / 龚阳辉
    /// 日期 : 2024-05-28 17:11
    /// 功能 : 与 Slider 有区分；包涵 进度图片 Bar ，进度标题 TextTitle 和 进度值 TextValue
    /// </summary>
    [DisallowMultipleComponent]
    public class ExProgressBar : ExMono
    {
        public enum ExProgressBarValueType
        {
            [Tooltip("自定义")]
            None,
            [Tooltip("百分号-2小数")]
            Percentage,
            [Tooltip("百分号-无小数")]
            PercentageF0,
            [Tooltip("当前值/最大值")]
            ValueMax,
        }

        protected ExProgressBar() : base()
        {
            /*
            if (this.progressBar != null)
            {
                this.value = this.progressBar.fillAmount * (this.maxValue - this.minValue);
            }
            */
        }

        public ExProgressBarValueType valueType = ExProgressBarValueType.None;

        [SerializeField]
        private ExImage _backGround;
        public ExImage backGround { get => this._backGround; set => this._backGround = value; }

        [SerializeField]
        private ExImage _progressBar;
        public ExImage progressBar { get => this._progressBar; set => this._progressBar = value; }

        [SerializeField]
        private ExImage _progressPreviewBar;
        /// <summary>
        /// 预览进度
        /// </summary>
        public ExImage progressPreviewBar { get => this._progressPreviewBar; set => this._progressPreviewBar = value; }

        [SerializeField]
        private ExText _textTitle;
        public ExText textTitle { get => this._textTitle; set => this._textTitle = value; }

        [SerializeField]
        private ExText _textValue;
        public ExText textValue { get => this._textValue; set => this._textValue = value; }

        private string _titleText = null;
        public string titleText
        {
            get { return this._titleText; }
            set
            {
                if (string.Equals(this._titleText, value))
                    return;
                this._titleText = value;

                if (this.textTitle != null)
                    this.textTitle.text = value;
            }
        }

        private string _text = null;
        public string text
        {
            get { return this._text; }
            set
            {
                if (string.Equals(this._text, value))
                    return;
                this._text = value;

                if (this.textValue != null)
                    this.textValue.text = value;
            }
        }


        public float minValue { get; set; } = 0f;
        public float maxValue { get; set; } = 1f;

        float _value = -1f;
        float _rate = 0f;
        public float value
        {
            get
            {
                return this._value;
            }
            set
            {
                float v2 = Mathf.Clamp(value, this.minValue, this.maxValue);
                bool isChanged = this._value != v2;
                this._value = v2;
                float diff = this.maxValue - this.minValue;
                float rate = diff == 0 ? 0 : (v2 / diff);
                isChanged = isChanged || this._rate != rate;
                this._rate = rate;
                if (isChanged)
                {
                    if (this.progressBar != null)
                        this.progressBar.fillAmount = rate;
                    this.OnValueChange();
                }
            }
        }

        float _valuePreview = -1f;
        float _ratePreview = 0f;
        public float valuePreview
        {
            get
            {
                return this._valuePreview;
            }
            set
            {
                float v2 = Mathf.Clamp(value, this.minValue, this.maxValue);
                bool isChanged = this._valuePreview != v2;
                this._valuePreview = v2;
                float diff = this.maxValue - this.minValue;
                float rate = diff == 0 ? 0 : (v2 / diff);
                isChanged = isChanged || this._ratePreview != rate;
                this._ratePreview = rate;
                if (isChanged)
                {
                    if (this.progressPreviewBar != null)
                        this.progressPreviewBar.fillAmount = rate;
                }
            }
        }

        private void OnValueChange()
        {
            this.ResetTextValue();
        }

        private void ResetTextValue()
        {
            switch (this.valueType)
            {
                case ExProgressBarValueType.None:
                    break;
                case ExProgressBarValueType.Percentage:
                case ExProgressBarValueType.PercentageF0:
                    float diff = this.maxValue - this.minValue;
                    float rate = (this.value / diff);
                    string rateStr = this.valueType == ExProgressBarValueType.Percentage ? rate.ToString("F2") : rate.ToString("F0");
                    this.text = rateStr + "%";
                    break;
                case ExProgressBarValueType.ValueMax:
                    this.text = $"{this.value}/{this.maxValue}";
                    break;
                default:
                    break;
            }
        }

        public ExProgressBar SetMinMax(float min, float max)
        {
            this.minValue = min;
            this.maxValue = max;
            return this;
        }

        public ExProgressBar SetValue(float value, float max, float min = 0)
        {
            this.SetMinMax(min, max);
            this.value = value;
            return this;
        }

        public ExProgressBar SetPreviewValue(float value)
        {
            this.valuePreview = value;
            return this;
        }
    }
}
